﻿using System.Web.Mvc;
using Website.Interfaces;

namespace Website.Infrastructure
{
    public class RedirectFilterAttribute : ActionFilterAttribute
    {
        private IRedirectionManager _redirectManager;

        public RedirectFilterAttribute(IRedirectionManager redirectManager)
        {
            _redirectManager = redirectManager;
        }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var request = filterContext.RequestContext.HttpContext.Request;

            if (_redirectManager.RedirectionRequired(request))
            {
                var url = _redirectManager.GetAlternateSiteUrl(request);
                filterContext.Result = new RedirectResult(url);

                return;
            }

            base.OnActionExecuting(filterContext);
        }
    }
}